package com.jerry.user.controller;

import com.github.pagehelper.PageInfo;
import com.jerry.annotation.CurrentUser;
import com.jerry.common.vo.PageModel;
import com.jerry.token.TokenManager;
import com.jerry.common.constant.MediaTypes;
import com.jerry.common.vo.ResultModel;
import com.jerry.user.entity.User;
import com.jerry.user.service.UserService;
import com.jerry.token.model.TokenModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Filename:    UserController
 * Description:
 * Copyright:   Copyright (c)2016
 * Company:     lngtop
 *
 * @author: 谢余锋
 * @version: 1.0
 * Create at:   2016/7/8 10:30
 * <p/>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2016/7/8      谢余锋        1.0        1.0 Version
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * @api {GET} path 11111
     * @apiGroup name
     * @apiName name
     * @apiDescription text socurceCode:
     * @apiVersion 0.0.1
     * @apiParam {Number} field descriptionoptional
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public User view(@PathVariable("id") String id) {
        return userService.getById(id);
    }

    @RequestMapping("/save")
    public ResultModel saveUser(@CurrentUser TokenModel tokenModel, @RequestBody User user) {
        userService.saveUser(user);
        return ResultModel.success("");
    }

    @RequestMapping("/page")
    public PageModel<User> page(@RequestParam(required = false, defaultValue = "1") int page,
                                @RequestParam(required = false, defaultValue = "10") int pageSize) {
        return userService.getUserPage(page, pageSize);
    }

    @RequestMapping("/list")
    public List<User> listUser() {
        return userService.queryAll();
    }
}
